package com.hgz.sbj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.hgz.sbj.mapper.TFxItemMapper;
import com.hgz.sbj.mapper.TFxRecordMapper;
import com.hgz.sbj.mapper.TFxTextMapper;
import com.hgz.sbj.model.TFxRecord;
import com.hgz.sbj.model.TFxRecordExample;
import com.hgz.sbj.model.TFxRecordExample.Criteria;
import com.hgz.sbj.model.TFxText;
import com.hgz.sbj.util.BaseUtil;

@Service
public class TFxRecordService {
	
	@Autowired
	private TFxRecordMapper tFxRecordMapper;
	
	@Autowired
	private TFxItemMapper tFxItemMapper;
	
	@Autowired
	private TFxTextMapper tFxTextMapper;
	
	public List<TFxRecord> getAllTFxRecords() throws Exception{
		TFxRecordExample example = new TFxRecordExample();
		example.setOrderByClause("open_time desc");
		return tFxRecordMapper.selectRecordsByExample(example);
	}
	
	public TFxText getText(String fxId) throws Exception{
		return tFxTextMapper.findText(fxId);
	}
	
	public boolean isExistRecord(Integer recordId) throws Exception{
		TFxRecordExample example = new TFxRecordExample();
		Criteria sql = example.createCriteria();
		sql.andRecordIdEqualTo(recordId);
		int count = tFxRecordMapper.countByExample(example);
		if(count != 0){
			return true;
		}
		return false;
	}
	
	
	
	public List<TFxRecord> readExcel(MultipartFile mFile, String rootPath, int startrow, int startcol) {
		List<TFxRecord> records = new ArrayList<TFxRecord>();
		try {
			XSSFWorkbook wb = new XSSFWorkbook(mFile.getInputStream());
			XSSFSheet sheet = wb.getSheetAt(0);
			//sheet 从0开始
			int rowNum = sheet.getLastRowNum() + 1; 
			
			XSSFCell thirdCell = null;
			for (int i = startrow; i < rowNum; i++) {
				//行循环开始
				TFxRecord record = new TFxRecord();
				XSSFRow row = sheet.getRow(i); 
				
				thirdCell = row.getCell(2);
				if(thirdCell.getCellType()!=XSSFCell.CELL_TYPE_STRING || 
						(!("buy".equals(thirdCell.getStringCellValue().trim()) || "sell".equals(thirdCell.getStringCellValue().trim())))){
					continue;
				}
				int  recordId = (int)row.getCell(0).getNumericCellValue();
				
				if(isExistRecord(recordId)){
					continue;
				}
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
				Date openTime = sdf.parse(row.getCell(1).toString().trim());
				String type = row.getCell(2).getStringCellValue();
				double size = row.getCell(3).getNumericCellValue();
				String item = row.getCell(4).getStringCellValue();
				double openPrice = row.getCell(5).getNumericCellValue();
				double stopLoss = row.getCell(6).getNumericCellValue();
				double targetProfit = row.getCell(7).getNumericCellValue();
				Date closeTime = sdf.parse(row.getCell(8).toString());
				double closePrice = row.getCell(9).getNumericCellValue();
				double commission = row.getCell(10).getNumericCellValue();
				double taxes = row.getCell(11).getNumericCellValue();
				double swap = row.getCell(12).getNumericCellValue();
				double profit = row.getCell(13).getNumericCellValue();
				
				record.setRecordId(recordId);
				record.setOpenTime(openTime);
				record.setFxType(type);
				record.setSize(BigDecimal.valueOf(size));
				record.setItemId(BaseUtil.convertItem(tFxItemMapper, item));
				record.setOpenPrice(BigDecimal.valueOf(openPrice));
				record.setStopLoss(BigDecimal.valueOf(stopLoss));
				record.setTargetProfit(BigDecimal.valueOf(targetProfit));
				record.setCloseTime(closeTime);
				record.setClosePrice(BigDecimal.valueOf(closePrice));
				record.setCommission(BigDecimal.valueOf(commission));
				record.setTaxes(BigDecimal.valueOf(taxes));
				record.setSwap(BigDecimal.valueOf(swap));
				record.setProfit(BigDecimal.valueOf(profit));
				
				tFxRecordMapper.insert(record);
				records.add(record);
			}	
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return records;
	}
}
